<?php

/**
 * Return contacts in a folder or all contacts for
 * contact list.
 * 
 * @author Volker Kueffel <volker.kueffel@gmail.com>
 * @version $Id: store_contact.php 33 2009-02-10 07:23:57Z volker.kueffel $
 */

require_once('config.php');
require_once('LDAPConnector.class.php');
require_once('Utils.class.php');

$ldap=LDAPConnector::init($config);

$result=new StdClass();
$result->data=array();
$result->success=false;
$result->numResults=0;
if ($ldap) {
	
	$dn='';
	$isNew=false;
	if (array_key_exists('subdn',$_REQUEST) && $_REQUEST['subdn']) {
		$dn=$ldap->getFullDN($_REQUEST['subdn']);
	} elseif (array_key_exists('folderdn',$_REQUEST) && array_key_exists('cn',$_REQUEST) && $_REQUEST['cn']) {
		$dn=$_REQUEST['folderdn']?$ldap->getFullDN($_REQUEST['folderdn']):$ldap->getBaseDN();
		$dn='cn='.$_REQUEST['cn'].','.$dn;
		$isNew=true;
	} 

	if ($dn) {
		$fields=Utils::request2LdapArray($_REQUEST);
		if (!$isNew) {
			$result->success=$ldap->modify($dn,$fields);
		} else {
			$result->success=$ldap->addContact($dn,$fields);			
		}
	}
}
echo json_encode($result);
?>